package com.mojito.learn.module.mysql;

import com.mojito.learn.business.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 生成大数据测试 MySQL 调优
 *
 * @author liufq
 * @since 2022/3/4 下午2:03
 */
@RestController
@RequestMapping("/mysql")
@Slf4j
public class MysqlController {

    @Resource
    private UserService userService;

    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * 批量插入用户数据
     */
    @PostMapping
    public void save(@RequestParam(defaultValue = "1000") Integer count) {
        userService.randomSave(count);
    }

    /**
     * 索引测试
     */
    @GetMapping("/index")
    public String index() {
        long start = System.currentTimeMillis();
        jdbcTemplate.query("select * from demo_index where score < 20 limit 50", (set) -> {
        });
        long end = System.currentTimeMillis();
        log.info("调用事件：{}", end - start);
        return "success";
    }
}
